package com.studykun.client.util;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.studykun.client.model.Teacher;
import com.studykun.client.model.World;
import com.studykun.client.ui.SimplePane;

public class TeachersPane extends SimplePane {
  final HorizontalPanel toolbar_ = new HorizontalPanel();
  final VerticalPanel verticalPanel_ = new VerticalPanel();
  final ArrayList<Teacher> selectedTeachers_ = new ArrayList<Teacher>();
  
  public TeachersPane(
      final World world) {
    this.setText("Teachers");
    this.setWidget(verticalPanel_);
    
    final FlexTable flexTable = new FlexTable();
    int index = 0;
    for (final Teacher teacher : world.teachers()) {
      final CheckBox checkBox = new CheckBox();
      checkBox.addClickHandler(new ClickHandler() {
        public void onClick(final ClickEvent event) {
          if (checkBox.getValue()) {
            selectedTeachers_.add(teacher);
          } else {
            selectedTeachers_.remove(teacher);
          }
        }
      });
      final InlineLabel teacherName =
        new InlineLabel(teacher.getAvatar().avatarName());
      final Room room = world.getRoom();
      if (room != null) {
        if (room.hasPresent(teacher)) {
          teacherName.setStyleName("present");
        }
      }
      final InlineLabel teacherDescription =
        new InlineLabel(teacher.description());
      flexTable.setWidget(index, 0, checkBox);
      flexTable.setWidget(index, 1, teacherName);
      flexTable.setWidget(index, 2, teacherDescription);
      ++index;
    }
    
    final Button button = new Button("Close");
    button.addClickHandler(new ClickHandler() {
      public void onClick(final ClickEvent event) {
        TeachersPane.this.close();
      }
    });

    verticalPanel_.add(toolbar_);
    verticalPanel_.add(flexTable);
    verticalPanel_.add(button);
    
    final Anchor invite = new Anchor("invite");
    toolbar_.add(invite);
    invite.addClickHandler(new ClickHandler() {
      public void onClick(final ClickEvent event) {
        final Room room = world.getRoom();
        if (room != null) {
          if (selectedTeachers_.size() == 1) {
            room.admit(selectedTeachers_.get(0));
          }
        }
      }
    });
  }
}
